﻿Imports System.Data.SqlClient
Public Class Paginacion
    Inherits System.Web.UI.Page
    Private dvworden As DataView
    Dim total As Double
    Dim conecta As conectar = New conectar

    Private Sub EnlazarGridview(ByVal gvw As GridView, ByVal dvw As DataView)
        With gvw
            .DataSource = dvw
            .DataBind()
        End With
    End Sub
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        Dim conecta As conectar = New conectar
        Try
            Me.Page.MaintainScrollPositionOnPostBack = True
            If Page.IsPostBack Then
                Using con As New SqlConnection(conecta.connstring)
                    Using dap As New SqlDataAdapter("", con)
                        Dim dst As New DataSet
                        con.Open()
                        dap.SelectCommand.CommandText = "select * from factura where observacionfactura='" & DropDownList1.SelectedItem.Text & "'" ' where codigo = " & txt_codigo.Text & ""
                        dap.Fill(dst, "factura")
                        dvworden = dst.Tables(0).DefaultView
                        Cache("factura") = dvworden
                        EnlazarGridview(GridView1, dvworden)
                    End Using
                End Using
            End If
        Catch ex As Exception

        End Try
    End Sub

    Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging
        'recuperar la vista de ordenes de la memoria cache
        dvworden = Cache("factura")
        'especificar como la pagina actual es la seleccionada
        GridView1.PageIndex = e.NewPageIndex
        EnlazarGridview(GridView1, dvworden)
    End Sub

    Protected Sub GridView1_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles GridView1.Sorting
        dvworden = Cache("factura")
        Dim campo As String = e.SortExpression
        If ViewState(campo) IsNot Nothing Then
            e.SortDirection = ViewState(campo)
        Else
            e.SortDirection = SortDirection.Descending

        End If

        If e.SortDirection = SortDirection.Descending Then
            dvworden.Sort = e.SortExpression & " asc"
            e.SortDirection = SortDirection.Ascending
        Else
            dvworden.Sort = e.SortExpression & " desc"
            e.SortDirection = SortDirection.Descending

        End If
        EnlazarGridview(GridView1, dvworden)
        ViewState(campo) = e.SortDirection
    End Sub

 
    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            total = total + e.Row.Cells(6).Text
        End If
        If e.Row.RowType = DataControlRowType.Footer Then
            e.Row.Cells(5).Text = "Total Monto Asegurado:"
            e.Row.Cells(6).Text = total.ToString("#,##0.00")
            e.Row.Cells(6).HorizontalAlign = HorizontalAlign.Left
            e.Row.Font.Bold = True
        End If
    End Sub

    Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles GridView1.SelectedIndexChanged

    End Sub
End Class